<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>\
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="box">
    <h2>登录</h2>
    <div class="input-box">
        <label>账号</label>
        <input type="text" id="username">
    </div>
    <div class="input-box">
        <label>密码</label>
        <input type="password" id="password">
    </div>
    <div class="btn-box">
        <div>
            <button id="submit">登录</button>
            <button id="signing">注册</button>
        </div>
    </div>
</div>

<!-- 引入 jquery -->
<script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

<!-- 实现前后端交互逻辑 -->
<script>
    let submitButton = document.querySelector("#submit");
    let sign = document.querySelector("#signing");

    sign.onclick = function () {
        location.assign('/register.html')
    }

    submitButton.onclick = function() {
        // 1. 获取到输入框的内容
        let username = document.querySelector("#username").value;
        let password = document.querySelector("#password").value;
        if(username === "" || password === "") {
            alert("账号或者密码为空");
            return;
        }


        // 2. 发送ajax请求
        $.ajax({
            url: '/login',
            type: 'post',
            data: {
                username: username,
                password: password
            },
            success: function(user) {
                // 3. 处理响应
                if (user && user.userId > 0) {
                    // 登录成功
                    // alert("登录成功");
                    // 跳转到聊天页面
                    location.assign('/client.html');
                }else {
                    alert("登录失败");
                }
            }
        });
    }
</script>



</body>
</html>